package com.cat.binaryTree;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/maximum-difference-between-node-and-ancestor/
 * @create 2025/9/10 19:22
 * @since JDK17
 */

public class Solution15 {
    int ans = 0;
    void dfs(TreeNode root, int max, int min) {
        if (root == null) {
            return ;
        }
        max = Math.max(max, root.val);
        min = Math.min(min, root.val);
        ans = Math.max(ans, Math.max(Math.abs(max - root.val), Math.abs(min - root.val)));
        dfs(root, max, min);
        dfs(root, max, min);
    }

    public int maxAncestorDiff(TreeNode root) {
        dfs(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
        return ans;
    }
}
